// ==UserScript==
// @name         RankQuery
// @namespace    http://d-quant.com/
// @version      0.2
// @description  query the rank of stock order
// @author       Souleep
// @match        http://125.93.72.197:4064/PubReport/ReportServer?reportlet=QryRankList.cpt
// @grant        everyone
// @updateURL    https://gitee.com/michaelsouleep/RankQuery/blob/master/Rankquery.js
// ==/UserScript==

    function FillSpace(source,len) {
        if(source.length<len)
        {
            var nLen=len-source.length;
            for(var i=0;i<nLen;i++)
            {
                source+=" ";
            }
        }
        return source;
    }

var tbHd=new Array("A2","B2","C2","D2","E2","F2");
var result="";
var inputStr="";
var fileInited=false;
var newInput=document.createElement('input');
function DoQry(strss,idx)
{
			console.log("=============");
			var subStr=strss[idx];
			try
			{
                var subSS=subStr.split(",");
				if(subSS.length>=2)
				{
					document.getElementsByName("SECURITYID")[0].value=subSS[0];
					document.getElementsByName("WTSL")[0].value=subSS[1]+"00";
					document.getElementById("fr-btn-SEARCH").click();
					setTimeout(function(){
						var table=document.getElementsByClassName("x-table")[0];
						if(table!=null)
						{
							var tableID = table.getAttribute("id");
							var str="";
							for(var i in tbHd)
							{
								var tbID=tbHd[i]+"-0-"+tableID;
								var ctrl=document.getElementById(tbID);
								if(ctrl!=null)
								{
									var tmpStr=ctrl.innerHTML;
									if(tmpStr=="买")
									{
										tmpStr="0";
									}
									else if(tmpStr=="卖")
									{
										tmpStr="1";
									}
									if(tmpStr.length>=3)
									{
										tmpStr=FillSpace(tmpStr,9);
									}
									else
									{
										tmpStr=FillSpace(tmpStr,3);
									}
									str+=tmpStr;
								}
							}
							result+=str+"\r\n";
							// console.log(str);
						}
					idx++;
					if(idx<strss.length)
					{
						DoQry(strss,idx);
					}
					else
					{
						var container=document.getElementsByClassName("view-container")[0];
						// console.log(result);
						var divElement = document.createElement("textarea");
						divElement.innerHTML=result;
						divElement.rows=15;
						divElement.cols=65;
						divElement.setAttribute("style","visibility:visible;margin-top:350px;margin-left:80px;z-index=100");
						container.appendChild(divElement);
						return;
					}
					}, 800);

					}
			}
			catch(err)
			{
				console.log(err);
			}
}

function StartQry(str)
{
	fileInited=true;
	inputSTR=str;

	// console.log(window.inputSTR);
	var ss=inputSTR.split("\n");
	DoQry(ss,0);
}

function BorwserFile()
{
	console.log(fileInited);
	if(fileInited==false)
	{
		setTimeout(function(){
			newInput.setAttribute('id','_ef');
			newInput.setAttribute('type','file');
			newInput.setAttribute("style",'visibility:hidden');
			newInput.value="";
			document.body.appendChild(newInput);
			newInput.click();
			newInput.onchange = function (ev) {
				console.log("-->"+newInput.value);
				var reader=new FileReader();
				reader.readAsText(newInput.files[0]);
				reader.onloadend=function(e)
				{
					StartQry(this.result);
				}
			}
			},50);

	}
	else
	{
        var reader=new FileReader();
				reader.readAsText(newInput.files[0]);
				reader.onloadend=function(e)
				{
					StartQry(this.result);
				}

	}
}
// BorwserFile();

document.onkeydown = function(e) {
        var keyCode = e.keyCode || e.which || e.charCode;
        var ctrlKey = e.ctrlKey || e.metaKey;
        if(ctrlKey && keyCode == 81) {
            BorwserFile();
        }
    }


